package constants

import (
	"fmt"
	"math/rand"
	"runtime"
	"time"

	ckeapi "cke.io/ckectl/app/apis"
)

var source *rand.Rand
var DeployContainerName string

func init() {
	source = rand.New(rand.NewSource(time.Now().UnixNano()))
	DeployContainerName = fmt.Sprintf("%s-%s", "cke-deploy-runner", RandomUID())
}

// ARCH get arch from runtime.GOARCH
var (
	archs = map[string]string{
		"arm64":    "arm64",
		"amd64":    "amd64",
		"mips64el": "mips64el",
		"mips64le": "mips64el",
	}

	ARCH = archs[runtime.GOARCH]
)

var (
	// ansible done files define diff done files for diff command
	AnsibleDoneFile = ""
)

const (
	// EtcdHealthcheckClientCertAndKeyBaseName defines etcd's healthcheck client certificate and key base name
	RootSshKeyName = "/root/.ssh/id_rsa"
	// EtcdHealthcheckClientCertName defines etcd's healthcheck client certificate name
	RootSshPublicKeyName = "/root/.ssh/id_rsa.pub"
	// EtcdHealthcheckClientKeyName defines etcd's healthcheck client key name
	RootSshAuthorizedKeyName = "/root/.ssh/authorized_keys"

	// Ansible log level: 1, 2 OR 3
	AnsibleLogLevel = 2

	// APICallRetryInterval defines how long kubeadm should wait before retrying a failed API operation
	DockerCallRetryInterval = 500 * time.Millisecond
	// TLSBootstrapTimeout specifies how long kubeadm should wait for the kubelet to perform the TLS Bootstrap
	DockerRestartTimeout = 5 * time.Minute

	// ModeRBAC is the mode to use Role Based Access Control to authorize
	AnsiblePlaybookCommand = "ansible-playbook"
	// ModeNode is an authorization mode that authorizes API requests made by kubelets.
	AnsibleConfig = "/opt/kubernetes-deploy/my_inventory/my_inventory.cfg"
	// ModeNode is an authorization mode that authorizes API requests made by kubelets.
	AnsibleAllConfig = "/opt/kubernetes-deploy/my_inventory/group_vars/all.yml"
	// APICallRetryInterval defines how long kubeadm should wait before retrying a failed API operation
	AnsibleLogDir = "/var/log/ckectl"
	// ModeRBAC is the mode to use Role Based Access Control to authorize
	AnsibleUpDoneFile = AnsibleLogDir + "/up.done"
	// ModeRBAC is the mode to use Role Based Access Control to authorize
	AnsibleScaleDoneFile = AnsibleLogDir + "/scale.done"
	// ModeRBAC is the mode to use Role Based Access Control to authorize
	AnsibleScaleDownDoneFile = AnsibleLogDir + "/scale.down.done"
	// ModeRBAC is the mode to use Role Based Access Control to authorize
	AnsibleUpdateDoneFile = AnsibleLogDir + "/update.done"
	// TLSBootstrapTimeout specifies how long kubeadm should wait for the kubelet to perform the TLS Bootstrap
	AnsibleLogFile = "ansible-playbook"
	// ModeRBAC is the mode to use Role Based Access Control to authorize
	DockerConfigDir = "/lib/systemd/system/docker.service.d"
	// ModeRBAC is the mode to use Role Based Access Control to authorize
	KubeadmDeployDir = "/opt/kubernetes-deploy"
	// TLSBootstrapTimeout specifies how long kubeadm should wait for the kubelet to perform
	AnsibleYmlDir = KubeadmDeployDir + "/ckectl"
	// Docker Service Patch for user, High Level Than /lib/systemd/system
	UserDockerServiceFile = "/etc/systemd/system/docker.service"
	DockerServiceFile     = "/lib/systemd/system/docker.service"
	// TLSBootstrapTimeout specifies how long kubeadm should wait for the kubelet to perform
	AnsibleTimeOut = "600"

	RFC3339AtNoZ = "2006-01-02@15:04:05"

	RootPrivateKey = "/root/.ssh/id_rsa"

	LogCacheSize = 1000
	PullImageInterval = 5 * time.Second
	PullImageTimeout  = 5 * time.Minute
)

func GetAnsibleImage(cfg *ckeapi.CkeConfiguration) string {
	return cfg.Ansible.AnsibleImageName
}

func RandomUID() string {
	var Len = 6
	var uuid = make([]byte, Len)

	for i := 0; i < Len; i++ {
		uuid[i] = byte(source.Intn(26) + 65 + 32)
	}
	return string(uuid)
}
